package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.OutboundDelivery;
import com.ggk911.pharmacy_system.service.impl.OutboundDeliveryServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 出库单
 *
 * @author ggk-911
 * @version 1.0 2023/4/13 12:51
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/outboundDelivery")
public class OutboundDeliveryController {

    private final OutboundDeliveryServiceImpl service;

    //GET查询
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               Integer drugInfoTypeId,
                               Integer outboundDeliveryNum,
                               @RequestParam(defaultValue = "") String drugInfoName,
                               @RequestParam(defaultValue = "") String manufacturer,
                               @RequestParam(defaultValue = "") String indications,
                               @RequestParam(defaultValue = "") String nationalCode,
                               @RequestParam(defaultValue = "") String approvalNo) {
        Page<OutboundDelivery> outboundDeliveryPage = new Page<>(page, size);
        QueryWrapper<OutboundDelivery> wrapper = new QueryWrapper<>();

        wrapper.eq(drugInfoTypeId != null, "drug_info_type.drug_info_type_id", drugInfoTypeId);
        wrapper.ge(outboundDeliveryNum != null, "outbound_delivery_num", outboundDeliveryNum);
        wrapper.and(!"".equals(drugInfoName), a -> a.like("drug_info.drug_info_name", drugInfoName).or().like("drug_info.drug_info_alias", drugInfoName));
        wrapper.like(!"".equals(manufacturer), "drug_info.drug_info_manufacturer", manufacturer);
        wrapper.like(!"".equals(indications), "drug_info.drug_info_indications", indications);
        wrapper.like(!"".equals(approvalNo), "drug_info.drug_info_approval_no", approvalNo);
        wrapper.like(!"".equals(nationalCode), "drug_info.drug_info_national_code", nationalCode);

        service.pageAdAd(outboundDeliveryPage, wrapper);

        return RestResult.ok("查询成功", outboundDeliveryPage);
    }

    //POST添加
    @PostMapping("/insert")
    public RestResult insert(OutboundDelivery outboundDelivery) {
        if (service.save(outboundDelivery)) {
            return RestResult.ok("添加成功");
        }
        return RestResult.error("添加失败");
    }

    //PUT修改
    @PutMapping("/update")
    public RestResult update(OutboundDelivery outboundDelivery) {
        if (service.updateById(outboundDelivery)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer id) {

        if (service.removeById(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }
}
